package com.kwin.excel;

import com.alibaba.excel.converters.Converter;
import com.kwin.excel.config.ExcelConfigProperties;
import com.kwin.excel.enhance.DefaultWriterBuilderEnhancer;
import com.kwin.excel.enhance.WriterBuilderEnhancer;
import com.kwin.excel.filter.ResponseExcelReturnValueHandler;
import com.kwin.excel.handler.ManySheetWriteHandler;
import com.kwin.excel.handler.SheetWriteHandler;
import com.kwin.excel.handler.SingleSheetWriteHandler;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @author : wangkai
 * @version V1.0
 * @Package com.kwin.excel
 * @Description:
 * @date 2021年10月19日 10:48
 **/
@RequiredArgsConstructor
@Configuration
public class ExcelHandlerConfiguration {
    
    private final ExcelConfigProperties configProperties;
    
    private final ObjectProvider<List<Converter<?>>> converterProvider;
    
    
    /**
     * ExcelBuild增强
     * @return DefaultWriterBuilderEnhancer 默认什么也不做的增强器
     */
    @Bean
    @ConditionalOnMissingBean
    public WriterBuilderEnhancer writerBuilderEnhancer() {
        return new DefaultWriterBuilderEnhancer();
    }
    
    
    /**
     * 单sheet 写入处理器
     */
    @Bean
    @ConditionalOnMissingBean
    public SingleSheetWriteHandler singleSheetWriteHandler() {
        return new SingleSheetWriteHandler(configProperties, converterProvider, writerBuilderEnhancer());
    }
    
    /**
     * 多sheet 写入处理器
     */
    @Bean
    @ConditionalOnMissingBean
    public ManySheetWriteHandler manySheetWriteHandler() {
        return new ManySheetWriteHandler(configProperties, converterProvider, writerBuilderEnhancer());
    }
    
    
    /**
     * 返回Excel文件的 response 处理器
     * @param sheetWriteHandlerList 页签写入处理器集合
     * @return ResponseExcelReturnValueHandler
     */
    @Bean
    @ConditionalOnMissingBean
    public ResponseExcelReturnValueHandler responseExcelReturnValueHandler(
        List<SheetWriteHandler> sheetWriteHandlerList) {
        return new ResponseExcelReturnValueHandler(sheetWriteHandlerList);
    }
    
}
